#include <iostream>
#include <mutex>
#include <thread>

class singleton
{
private:
    static singleton m_instance;
    singleton()
    {
    }
    ~singleton()
    {
    }
    singleton &operator=(const singleton &itance) = delete;
    singleton(singleton const &) = delete;

public:
    static singleton *GetInstanc()
    {
        return &m_instance;
    }
};

singleton singleton::m_instance;

class singleton_lazy
{
private:
    singleton_lazy() {};
    // 防拷贝
    singleton_lazy(singleton_lazy const &);
    singleton_lazy &operator=(singleton_lazy const &);

    static singleton_lazy *m_instance; // 单例对象指针
    static std::mutex m_mtx;           // 互斥锁
public:
    static singleton_lazy *Getinstance()
    {
        if (m_instance == nullptr)
        {
            m_mtx.lock();
            if (m_instance == nullptr)
            {
                m_instance = new singleton_lazy();
                return m_instance;
            }
            m_mtx.unlock();
        }
    }

    // 实现一个内嵌的垃圾回收机制
    class CGarbo
    {
    public:
        ~CGarbo()
        {
            if (singleton_lazy::m_instance)
                delete singleton_lazy::m_instance;
        }
    };

    static CGarbo CG;
};